package com.example.module_clocking.ui

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_common.base.BaseActivity
import com.example.lib_common.base.state.Empty
import com.example.lib_common.base.state.Failed
import com.example.lib_common.base.state.Loading
import com.example.lib_common.base.state.Success
import com.example.lib_common.observeWithLifecycle
import com.example.lib_common.utils.RouterPath
import com.example.module_clocking.R
import com.example.module_clocking.adapter.ClockingAdapter
import com.example.module_clocking.adapter.ClockingTypeAdapter
import com.example.module_clocking.databinding.ActivityClockingListBinding
import com.example.module_clocking.model.ClockListEntity
import com.example.module_clocking.viewModel.ClockingViewModel
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.CLOCKING_LIST_UI)
class ClockingListActivity : BaseActivity<ClockingViewModel, ActivityClockingListBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_clocking_list

    private lateinit var mAdapter: ClockingAdapter
    private lateinit var mAdapterType: ClockingTypeAdapter
    private val mViewModel: ClockingViewModel by viewModels()

    override fun initView() {
        super.initView()
        mAdapter= ClockingAdapter()
        mAdapterType = ClockingTypeAdapter()
        mViewModel.getClockingList(MMKV.defaultMMKV().decodeInt("userid"))
        mViewModel.initList()
        mBinding.rvClockingList.layoutManager=LinearLayoutManager(this)
        mBinding.rvClockingList.adapter=mAdapter
        initAdapter()
        // 初始化选中状态
        mBinding.rgStatus.check(R.id.rb_all)

        // 处理选项变更
        mBinding.rgStatus.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.rb_all -> updateUIForAll()
                R.id.rb_normal -> updateUIForStatus("正常")
                R.id.rb_late -> updateUIForStatus("迟到")
                R.id.rb_leave_early -> updateUIForStatus("早退")
                R.id.rb_absent -> updateUIForStatus("缺勤")
            }

            // 重置"全部"选项的样式
            resetAllOptionStyle(checkedId != R.id.rb_all)
        }


    }

    override fun initData() {

    }

    private fun updateUIForAll() {
        // 显示所有考勤记录
    }

    private fun updateUIForStatus(status: String) {
        // 根据选中的状态过滤考勤记录
    }
    private fun resetAllOptionStyle(isUnselected: Boolean) {
        mBinding.rbAll.background = resources.getDrawable(
            if (isUnselected) R.drawable.bg_status_unselected else R.drawable.bg_status_selected,
            theme
        )
        mBinding.rbAll.setTextColor(
            resources.getColor(
                if (isUnselected) R.color.black else R.color.white,
                theme
            )
        )
    }
    override fun initListener() {
        super.initListener()
        mViewModel.clockingState.observeWithLifecycle(this){
            when(it){
                Empty->{

                }
                is Failed->{

                }
                Success->{
                    mAdapter.setData(mViewModel.clockingList)
                    mAdapterType.setData(mViewModel.ClockType)

                }
                Loading->{

                }
            }
        }
    }

    private fun initAdapter(){


        mAdapter.setData(mViewModel.clockingList)
        mBinding.rvClockingList.layoutManager=LinearLayoutManager(this)
        mBinding.rvClockingList.adapter=mAdapter
    }

}